import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Main from '../views/Main.vue'
import Register from '../views/Register.vue'
import MenuManagement from '../views/sys/MenuManagement.vue'
import RoleManagement from '../views/sys/RoleManagement.vue'
import UserManagement from '../views/sys/UserManagement.vue'
import UserInfo from '../views/sys/UserInfo.vue'
import Pass from '../views/sys/Pass.vue'
import StaffList from '../views/sys/StaffList.vue'
import GoodsList from '../views/sys/GoodsList.vue'
import ShoppingCart from '../views/sys/ShoppingCart.vue'
import OrderList from '../views/sys/OrderList.vue'

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [{
        path: '/',
        redirect: '/login'
    }, {
        path: '/login',
        component: Login
    }, {
        path: '/shop',
        component: ShoppingCart
    }, {
        path: '/main',
        component: Main,
        children: [{
            path: '/menu',
            component: MenuManagement
        }, {
            path: '/role',
            component: RoleManagement
        }, {
            path: '/user',
            component: UserManagement
        }, {
            path: '/userinfo',
            component: UserInfo
        }, {
            path: '/pass',
            component: Pass
        }, {
            path: '/emp',
            component: StaffList
        }, {
            path: '/goods',
            component: GoodsList
        }, {
            path: '/order',
            component: OrderList
        }]
    }, {
        path: '/register',
        component: Register
    }]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // from 从哪个路径而来
    // next 表示函数
    // next() 表示放行 next('/login') 强制跳转
    if (to.path === '/login' || to.path === '/register') return next()
    const tokenStr = window.localStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

export default router